import sun.applet.AppletAudioClip;

import javax.swing.*;
import java.applet.AudioClip;
import java.awt.event.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @classname
 * @Author 姬如千泷
 * @Date 2021/4/29  14:39
 * @Version 1.0
 */
class MyListener implements ActionListener, KeyListener {
    MyCharacter ct_label;

    public long lastPress=0;
    Set<Integer> array = new HashSet<>();
    AudioClip jump_voice = new AppletAudioClip(DateCenter.jump_voiceUrl);
    AudioClip jumpx_voice = new AppletAudioClip(DateCenter.jumpx_voiceUrl);
    AudioClip gFlash_voice = new AppletAudioClip(DateCenter.gFlash_voiceUrl);
    AudioClip broach_voice = new AppletAudioClip(DateCenter.broach_voiceUrl);
    AudioClip attack_voice = new AppletAudioClip(DateCenter.attack_voiceUrl);
    AudioClip godX_voice = new AppletAudioClip(DateCenter.godX_voiceUrl);
    public MyListener(MyCharacter ct_label) {
        this.ct_label = ct_label;
    }
    public void windowsListen(JFrame a){
        a.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
    @Override
    public void actionPerformed(ActionEvent e) {
    }
    @Override
    public void keyTyped(KeyEvent e) {
    }
    //键盘按压监听
    @Override
    public void keyPressed(KeyEvent e) {
        array.add(e.getKeyCode());//维护一组键值
        if(System.currentTimeMillis()-lastPress>50&&array.size()>0) {
            for (Integer integer : array) {
                switch (integer) {
                    case KeyEvent.VK_RIGHT: {
                        ct_label.rmove(ct_label.ct_y);
                    }
                    break;
                    case KeyEvent.VK_UP: {
                        if (ct_label.ct_y>=350&&!ct_label.juage_jump&&!ct_label.juage_attack&&!ct_label.juage_skill) {//到界面边界
                            ct_label.ct_y -= 10;
                        }
                        ct_label.juage_stright = true;
                        if(ct_label.juage_lr)
                            ct_label.lmove(ct_label.ct_y);
                        else ct_label.rmove(ct_label.ct_y);
                    }
                    break;
                    case KeyEvent.VK_DOWN: {
                        if (ct_label.ct_y<=520&&!ct_label.juage_jump&&!ct_label.juage_attack&&!ct_label.juage_skill) {//到界面边界
                            ct_label.ct_y += 10;
                        }
                        ct_label.juage_stright = true;
                        if(ct_label.juage_lr)
                            ct_label.lmove(ct_label.ct_y);
                        else ct_label.rmove(ct_label.ct_y);
                    }
                    break;
                    case KeyEvent.VK_LEFT: {
                        ct_label.lmove(ct_label.ct_y);
                    }
                    break;
                    case KeyEvent.VK_SPACE:{//跳跃
                        jump_voice.play();
                        if(ct_label.juage_lr)
                            ct_label.ljump();
                        else ct_label.rjump();
                    }break;
                    case KeyEvent.VK_X:{
                        if(ct_label.juage_jump){//跳跃斩击
                            ct_label.juage_jumpx =true;
                            jumpx_voice.play();
                        }
                        else {//普通攻击
                            attack_voice.play();
                            if(ct_label.juage_lr)
                                ct_label.lattack();
                            else ct_label.rattack();
                        }
                    }break;
                    case KeyEvent.VK_W:{//拔刀斩
                        if(!ct_label.juage_jump&&!ct_label.juage_attack&&!ct_label.juage_skill) {
                            broach_voice.play();
                            ct_label.juage_skill = true;
                            new MySkill(ct_label).broach();
                        }
                    }break;
                    case KeyEvent.VK_E:{//鬼影闪
                        if(!ct_label.juage_jump&&!ct_label.juage_attack&&!ct_label.juage_skill) {
                            gFlash_voice.play();
                            ct_label.juage_skill = true;
                            new MySkill(ct_label).gFlash();
                        }
                    }break;
                    case KeyEvent.VK_S:{//圣灵斩击
                        if(!ct_label.juage_jump&&!ct_label.juage_attack&&!ct_label.juage_skill) {
                            godX_voice.play();
                            ct_label.juage_skill = true;
                            new MySkill(ct_label).godX();
                        }
                    }break;
                    case KeyEvent.VK_SHIFT:{//跑步
                        ct_label.juage_run=!ct_label.juage_run;
                    }
                }
            }
            lastPress=System.currentTimeMillis();
        }
    }
    @Override
    public void keyReleased(KeyEvent e) {

        if(ct_label.juage_lr&&!ct_label.juage_jump&&!ct_label.juage_attack&&!ct_label.juage_skill)
            ct_label.setIcon(DateCenter.lstand);
        else if(!ct_label.juage_jump&&!ct_label.juage_attack&&!ct_label.juage_skill)
            ct_label.setIcon(DateCenter.rstand);
        ct_label.pressNum=0;
        array.remove(e.getKeyCode());
    }
}
